package com.sxbang.bms.dao;

import com.sxbang.bms.model.SysRole;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface RoleDao {

    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into sys_role(name, description, createTime, updateTime) values(#{name}, #{description}, now(), now())")
    int save(SysRole role);

    @Select("select * from sys_role t where t.id = #{id}")
    SysRole getById(Integer id);

    @Select("select * from sys_role t")
    List<SysRole> getAllRoles();

    int updateRole(SysRole role);

    @Delete("delete from sys_role where id = #{id}")
    int deleteRole(Integer id);

    @Select("select * from sys_role t where t.name = #{rolename}")
    SysRole getRole(String rolename);

    @Select("select * from sys_role t limit #{offset},#{limit}")
    List<SysRole> getByPage(@Param("offset") Integer offset, @Param("limit") Integer limit);

    @Select("select count(*) from sys_role t")
    Long countAll ();

    @Delete("delete from sys_role_permission where roleId = #{roleId}")
    int deleteRolePermission(Integer roleId);

    int saveRolePermission(@Param("roleId") Integer roleId, @Param("permissionIds") List<Long> permissionIds);

    @Select("select * from sys_role t where t.name like '%${roleName}%' limit #{startPosition},#{limit}")
    List<SysRole> getRoleByFuzzyRoleNamePage(@Param("roleName") String roleName,@Param("startPosition")Integer startPosition,@Param("limit")Integer limit);

    @Select("select count(*) from sys_role t where t.name like '%${roleName}%'")
    Long countRoleByFuzzyRoleName(@Param("roleName") String roleName);
}
